<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link rel="stylesheet" type="text/css" href="../js/themes/bootstrap/easyui.css">
	<link rel="stylesheet" type="text/css" href="../js/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="../demo/demo.css">
	<script type="text/javascript" src="../js/jquery.min.js"></script>
	<script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../js/locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript">		
	$(function(){
		//自定义1个combobox编辑器
		$.extend($.fn.datagrid.defaults.editors, {
		
			    combogrid: {			
			        init: function(container, options){			
			            var input = $('<input type="text" class="datagrid-editable-input">').appendTo(container);
			            input.combogrid(options);		
			            return input;			
			        },
		
			        destroy: function(target){			
			            $(target).combogrid('destroy');		
			        },
		
			        getValue: function(target){			
			            return $(target).combogrid('getValue');		
			        },
			
			        setValue: function(target, value){		
			            $(target).combogrid('setValue', value);			
			        },
			
			        resize: function(target, width){		
			            $(target).combogrid('resize',width);			
			        }
			
			    }
			
			});

		
		
		var ds =[
	{

	"ystype": "1",
	"ysname": "应收"
	},
	{
		"ystype": "0",
		"ysname": "应付"
	}
	];
		
		var as="";

		
			$.ajax({
				type:"GET",
		 		async: false,
				url:'<%=path%>/main/get/subinfo',
				dateType:'json',
				data:null,
				success:function(data){
					var result = eval('(' + data + ')');
					as=result.data;
				
					}
				});
		
			var cp="";
		$('#tt').datagrid({
			
			width:750,
			height:350,
			url:'<%=path%>/rm/selectrm',
			queryParams:{
				cropid:<%=session.getAttribute("cropid")%>
			},
			singleSelect:true,
			columns:[[
			{field:'id',title:'序号',width:190,checkbox:true},
	         {  
                field: 'type',  
                title: '类型(其他应收应付)',  
                width: 130,  
                formatter: function(value) {  
                    for (var i = 0; i < ds.length; i++) {  
                        if (ds[i].code == value){ 	
                        	return ds[i].code
                        };  
                    }  
                    return value;  
                },  
                editor: {  
                    type: 'combobox',  
                    options: {  
                        valueField: 'ysname',  
                        textField: 'ysname',  
                        data: ds,  
                        required: true,
                    }  
                }  
            },
            {field:'corp',title:'公司',name:'corp',width:100,            	
	            editor:{            	
	                type:'combogrid',           	
	                options:{            
	                    panelWidth:163,
	
	                    idField:'cAccId',

	                    textField:'cAccName',
	                    multiple:true,
	                    url:'<%=path %>/login/get/corps',

	                    columns:[[
	                        {field:'cAccId',title:'公司编码',width:60},           
	                        {field:'cAccName',title:'公司名称',width:100},
	                    ]],
	                	onSelect:function(rowIndex, rowData){
	                		cp="";
	                		cp+=cp==""?rowData.cAccName:","+rowData.cAccName;
							
						}

	                }

	            }
	        },

            {  
                field: 'subjectcode',  
                title: '科目编码',  
                width: 100,  
                formatter: function(value) {  
                    for (var i = 0; i < as.length; i++) {  
                        if (as[i].code == value){ 
                        	
                        	return as[i].code
                        };  
                    }  
                    return value;  
                },  
                editor: {  
                    type: 'combobox',  
                    options: {  
                        valueField: 'code',  
                        textField: 'code',  
                        data: as,  
                        required: true,
                        onSelect:function(rowIndex, rowData){
                  
                        	var row = $("#tt").datagrid('getSelected');
                        	var rindex = $("#tt").datagrid('getRowIndex', row);
                   
                        	var ed = $("#tt").datagrid('getEditor', {
                                index: rindex,
                                field: 'subjectcode'
                            });
                        	  var s = $(ed.target).combobox('getText');
                        	$.ajax({
                				type:"GET",
                		 		async: false,
                				url:'<%=path%>/main/get/subname',
                				dateType:'json',
                				data:{"subcode":s},
                				success:function(data){
                					
                					var result = eval('(' + data + ')');
                					var editor = $("#tt").datagrid("getEditor",{index:rindex,field:'subjectname'});
                	
                					$(editor.target).textbox('setValue',result.data.codename);
                					}
                				});
                        	}
                    }  
                }  
            },
			{field:'subjectname',title:'科目名称',width:195,editor: { type: 'textbox', options: { required: true } }},
	        {field:'action',title:'操作',width:90,align:'center',
                formatter:function(value,row,index){
                    if (row.editing){
                        var s = '<a href="#" onclick="saverow(this)">保存</a> ';
                        var c = '<a href="#" onclick="cancelrow(this)">取消</a>';
                        return s+c;
                    } else {
                        var e = '<a href="#" onclick="editrow(this)">编辑</a> ';
                        var d = '<a href="#" onclick="deleterow(this)">删除</a>';
                        return e+d;
                    }
                }
            }
			          ]],
			          onBeforeEdit:function(index,row){
				            row.editing = true;
				            updateActions(index);
				        },
				        onAfterEdit:function(index,row){
				            row.editing = false;
				            updateActions(index);
				        },
				        onCancelEdit:function(index,row){
				            row.editing = false;
				            updateActions(index);
				        },
				        onEndEdit:function(rowIndex, rowData, changes){
				        	rowData.corp = cp;	
				        	 
				        }
		});	
		

		
		//点击 getcheck按钮获取  选中checkbox的一行值
		$('#btn1').click(function(){
			
			var checkedItems = $('#tt').datagrid('getChecked');
			
			var sde="";
			var st="";
			var sb="";
			var sn="";
			
			$.each(checkedItems, function(index, item){
				sde+=item.type+",";
				st+=item.corp+",";
				sb+=item.subjectcode+",";
				sn+=item.subjectname+",";
				});
	
			if(sde=="undefined,"||st=="undefined,"||sb=="undefined,"||sn=="undefined,"){
				alert("请填写数据");
			}else{
		
		$.ajax({
				type:"GET",
		 		async: false,
				url:'<%=path%>/rm/insert/receivepay',
				dateType:'json',
				data:{"sde":sde,"st":st,"sb":sb,"sn":sn,"cropid":<%=session.getAttribute("cropid")%>},
				success:function(data){
					var result = eval('(' + data + ')');
					var num = result.status;
					if(num!=0){
						alert("保存成功");
						}
					}
				}); 
			}
		});
	});
	function updateActions(index){
	    $('#tt').datagrid('updateRow',{
	        index: index,
	        row:{}
	    });
	}
	
	function getRowIndex(target){
	    var tr = $(target).closest('tr.datagrid-row');
	    return parseInt(tr.attr('datagrid-row-index'));
	}
	function editrow(target){
	    $('#tt').datagrid('beginEdit', getRowIndex(target));
	}
	function deleterow(target){
		
		var s=$("#tt").datagrid('getChecked');
		console.log(s);
	    $.messager.confirm('','确定删除吗?',function(r){
	        if (r){
	       			var buytype = "";
	       			var corp ="";
					$.each(s,function(index, item){
							buytype=item.type;
							corp=item.corp;
						});
				
					 $.ajax({
							type:"GET",
					 		async: false,
							url:'<%=path%>/rm/del',
							dateType:'json',
							data:{"buytype":buytype,"corp":corp,"cropid":<%=session.getAttribute("cropid")%>},
							success:function(data){
								var num = data.status;
								if(num!=0){
									alert("删除成功!");	
								}
							}
							
						});	
	        	
	        	
	            $('#tt').datagrid('deleteRow', getRowIndex(target));
	        }
	    });
	}
	function saverow(target){
	    $('#tt').datagrid('endEdit', getRowIndex(target));
	}
	function cancelrow(target){
	    $('#tt').datagrid('cancelEdit', getRowIndex(target));
	}
	
	function insert(){
		var row = $('#tt').datagrid('getSelected');
		if (row){
			var index = $('#tt').datagrid('getRowIndex', row);
		} else {
			index = 0;
		}
		$('#tt').datagrid('insertRow', {
			index: index,
			row:{
				status:'P'
			}
		});
		$('#tt').datagrid('selectRow',index);
		$('#tt').datagrid('beginEdit',index);
	}
	</script>
<style>
	#d1{
		text-align:center;
	}
#d3{
	width: 750px;
	margin: 0 auto;
}

</style>
<title>Insert title here</title>


</head>
<body>
		<div id="d1"><h2>应收应付</h2></div>
		<div style="margin-left:465px" id="dd1">
			<span><a href="javascript:void(0)" class="easyui-linkbutton l-btn l-btn-small" onclick="insert()" group id>
				<span class="l-btn-left">
					<span class="l-btn-text">新增</span>
				</span>
			</a></span>
			<span><a href="javascript:void(0)" class="easyui-linkbutton l-btn l-btn-small">
				<span class="l-btn-left">
					<span class="l-btn-text" id='btn1'>保存</span>
				</span>
			</a></span>
		</div>
		<div id="d3">
			<table id="tt">
						
			</table>
		</div>
</body>
</html>